# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
import pandas as pd
import os

columns = ['loadingOrder','carrierName','timestamp','lon',
                  'lat','vesselMMSI','speed','direction','vesselNextport',
                  'vesselNextportETA','vesselStatus','vesselDatasource','TRANSPORT_TRACE']


df = pd.read_csv("~/huawei/test.txt", header=None)
df.columns = columns


port = pd.read_csv('~/huawei/port.csv').sort_values(by = 'LONGITUDE')

def get_history():
    ordered_path = '/home/ua17/huawei/loading_orders.csv'
    if os.path.exists(ordered_path):
        history = pd.read_csv(ordered_path)
        print("loading exists file")
    else:
        print("loading original file")
        history  = pd.read_csv('/home/ua17/huawei/loadingOrderEvent.csv')
        history.drop(history[history.loadingOrder.isna()].index, inplace=True)
        history.sort_values(by = ['loadingOrder', 'EVENT_CONVOLUTION_DATE'], inplace=True)
        history.to_csv(ordered_path, index=False)
    history['EVENT_CONVOLUTION_DATE'] = pd.to_datetime(history['EVENT_CONVOLUTION_DATE'], infer_datetime_format=True)
    return history    
    
def get_data(data, mode='train'):
    assert mode=='train' or mode=='test'
    
    if mode=='train':
        data['vesselNextportETA'] = pd.to_datetime(data['vesselNextportETA'], infer_datetime_format=True)
    elif mode=='test':
        data['temp_timestamp'] = data['timestamp']
        data['onboardDate'] = pd.to_datetime(data['onboardDate'], infer_datetime_format=True)
    
    for fl_col in ['lon', 'lat', 'speed', 'direction']:
        data[fl_col] = data[fl_col].astype(float)

    data['loadingOrder'] = data['loadingOrder'].astype(str)
    

    data[['lat2', 'lon2', 'timestamp2', 'speed2']] = \
        data.groupby('loadingOrder').shift(1)[['lat','lon', 'timestamp', 'speed']]
    
    for ts_col in ['timestamp', 'timestamp2']:
        data[ts_col] = pd.to_datetime(data[ts_col], infer_datetime_format=True)

    return data

df = get_data(df)

